package com.ideal.manage.esms.controller.monitor;

import com.alibaba.fastjson.JSONObject;
import com.ideal.manage.esms.bean.DTO.Result;
import com.ideal.manage.esms.bean.project.EquipmentManage;
import com.ideal.manage.esms.bean.project.ParameterInfo;
import com.ideal.manage.esms.service.monitor.MonitorService;
import com.ideal.manage.esms.service.project.EquipmentManageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @author Allen
 */
@Controller
@RequestMapping("monitor")
public class MonitorForGroupController {
    private Logger logger = LoggerFactory.getLogger(MonitorController.class);

    @Resource
    private MonitorService monitorService;
    @Resource
    private EquipmentManageService equipmentManageService;

    /**
     * 系统监控内嵌页面 - 电池模组
     *
     * @param model
     * @param id
     */
    @RequestMapping("monitor_web_for_group")
    public void monitorWebForgroup(Model model, String id) {
        model.addAttribute("id", id);
        // 1. 实体类（找到那个电池模组）
        EquipmentManage equipmentManage = equipmentManageService.findOne(id);
        model.addAttribute("equipmentManage", equipmentManage);
        if (StringUtils.isBlank(equipmentManage.getCode())) {
            model.addAttribute("message", "请在工程配置->对象管理->设备管理页面设置设备唯一标识，否则无法显示");
            return;
        }
        // 3. 模组监控,将参数测点配置的信息返回页面
        List<ParameterInfo> parameterInfoList = monitorService.findByListTypeForGroup(id, equipmentManage.getEquipmentType(), equipmentManage);
        model.addAttribute("parameterInfoList", parameterInfoList);
        // 4. 单体电池 电压 监控
          List<EquipmentManage> equipmentManages = monitorService.findBattery(id);
        model.addAttribute("equipmentManages", equipmentManages);
        // 5. 模组的电压下限、电压上限等
        JSONObject jsonObject = monitorService.getHighAndLow(id);
        model.addAttribute("jsonObject", jsonObject);
    }

    /**
     * top 3 / bottom 3
     * @param id
     * @return
     */
    @RequestMapping("get_voltage_group_data")
    @ResponseBody
    public List getVoltageGroupData(String id) {
        int size = 3;
        //找到对应的电池模组
        EquipmentManage equipmentManage = equipmentManageService.findOne(id);
        return monitorService.getVoltage(id,equipmentManage,size);
    }

    /**
     * 实时监控 - 电压/电流/SOC  今日曲线
     *
     * @param id
     * @return
     */
    @RequestMapping("get_group_today_line")
    @ResponseBody
    public Map getTodayLine(String id, Model model) throws ParseException {
        EquipmentManage equipmentManage = equipmentManageService.findOne(id);
        if (StringUtils.isBlank(equipmentManage.getCode())) {
            model.addAttribute("message", "请在工程配置->对象管理->设备管理页面设置设备唯一标识，否则无法显示");
            return null;
        } else {
            return  monitorService.getTodayLine(id);
        }

    }

    /**
     * 实时监控 - 电压/温度 今日极值/极差曲线
     *
     * @param id
     * @return
     */
    @RequestMapping("get_group_today_bar")
    @ResponseBody
    public Map getTodayBar(String id, Model model) throws ParseException {
        EquipmentManage equipmentManage = equipmentManageService.findOne(id);
        if (StringUtils.isBlank(equipmentManage.getCode())) {
            model.addAttribute("message", "请在工程配置->对象管理->设备管理页面设置设备唯一标识，否则无法显示");
            return null;
        } else {
            return monitorService.getTodayBar(id);
        }
    }

    /**
     * 保存 模组电压上下限等
     *
     * @param id
     * @param name
     * @param val
     */
    @RequestMapping("save_high_and_low")
    @ResponseBody
    public Result saveHighAndLow(String id, String name, Double val) {
        monitorService.saveHighAndLow(id, name, val);
        return new Result("success", "alert");
    }

    /**
     * 电池模组历史曲线
     * @param id
     * @param objType
     * @param name
     * @param ranges
     * @param freque
     * @param show_type
     * @param start_time
     * @param end_time
     * @return
     */
    @RequestMapping("history_group_data_equipment")
    @ResponseBody
    public Map historyDataEquipment(String id, int objType, String name, String ranges,
                                    String freque, String show_type, String start_time, String end_time) {
        if ("custom".equals(ranges) && StringUtils.isBlank(start_time) && StringUtils.isBlank(end_time)){
            return null;
        }
        return monitorService.groupBatteryHistoryDataEquipment(id, objType, name, ranges, freque, show_type, start_time, end_time);
    }
}
